define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'fastflowbase'], function ($, undefined, Backend, Table, Form, FastflowBase) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fastflow/bill/cooperation/index' + location.search,
                    add_url: 'fastflow/bill/cooperation/add',
                    edit_url: 'fastflow/bill/cooperation/edit',
                    del_url: 'fastflow/bill/cooperation/del',
                    multi_url: 'fastflow/bill/cooperation/multi',
                    import_url: 'fastflow/bill/cooperation/import',
                    table: 'cooperation',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='initiator_id']", form).addClass("selectpage").data("source", "fastflow/flow/flow/getSelectpageWorkers?scope=1").data("primaryKey", "id").data("field", "initiator").data("orderBy", "id asc");
                Form.events.selectpage(form);
            });

            table.on('post-body.bs.table', function (e, data) {
                $('#table').bootstrapTable('expandAllRows');
                $('#table').bootstrapTable('collapseAllRows');
            });
            table.on('load-success.bs.table', function (e, data) {
                FastflowBase.api.renderbadge();
            });

            $(window).resize( function  () {
                table.bootstrapTable('resetView');
            });
			var fixedColumnsEnable = true;
			if ($(window).width() < 600){
                fixedColumnsEnable = false;
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                detailView: true,
                detailFormatter: FastflowBase.api.formatter.detail,
                fixedColumns: fixedColumnsEnable,
                fixedleftNumber: 1,
                fixedNumber:3,
                rowStyle: function (row, index) {
                    return {
                        css: {
                            border: '1px solid #eaeaea'
                        }
                    };
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), sortable: true, formatter: FastflowBase.api.formatter.id},
                        {field: '', title: __('流程控制'), width:"250", operate: false, table: table, events: FastflowBase.api.events.flowinfo, formatter: FastflowBase.api.formatter.flowinfo},
                        {field: 'originator', title: __('甲方'), operate: 'LIKE'},
                        {field: 'partner', title: __('乙方'), operate: 'LIKE'},
                        {field: 'department.name', title: __('牵头部门'), operate: 'LIKE'},
                        {field: 'participating_departments', title: __('主要参与部门'), operate: 'LIKE'},
                        {field: 'cname', title: __('cname'), operate: 'LIKE'},
                        {field: 'type', title: __('类型'), searchList: {"0": __("校地合作"),"1": __('校企合作'),"2": __('校校合作'),"3": __('其他')}, formatter: Table.api.formatter.status, custom: {'0': 'warning', '1': 'success', '2': 'info', '3': 'danger'}},
                        {field: 'startdate', title: __('Startdate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'enddate', title: __('Enddate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'contacts', title: __('Contacts'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'counterpart', title: __('Counterpart'), operate: 'LIKE'},
                        {field: 'counterpart_mobile', title: __('Counterpart_mobile')},
                        {field: 'agreementfile', title: __('Agreementfile'), operate: false, formatter: Table.api.formatter.file},
                        {field: 'agreement', title: __('Agreement'), operate: false, formatter: Table.api.formatter.file},
                        {field: 'scansion', title: __('Scansion'), operate: false, formatter: Table.api.formatter.file},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {field: 'initiator_id', title: __('Initiator_id'), visible: false},
                        {field: 'createusername', title: __('流程发起人'), operate: false},
                        {field: 'status', title: __('状态'), searchList: {"0": __('Status 0'),"1": __('Status 1'),"2": __('Status 2'),"3": __('Status 3')}, formatter: Table.api.formatter.status, custom: {'0': 'warning', '1': 'success', '2': 'info', '3': 'danger'}},
                        {field: 'flow_progress', title: __('流程进度'), operate: false, formatter: FastflowBase.api.formatter.progress},
                        //{field: '', title: __('流程控制'), width:"250", operate: false, table: table, events: FastflowBase.api.events.flowinfo, formatter: FastflowBase.api.formatter.flowinfo},
                    ]
                ]
            });

            $(".btn-check").click(function() {
                var ids = Table.api.selectedids(table)
                console.log(ids)
                if(ids.length>0)
                    layer.confirm("确定考核？", function() {
                        $.ajax({
                            url: "fastflow/bill/cooperation/check",
                            data: {
                                ids: ids
                            },
                            success(res) {
                                if (res == 1) {
                                    layer.alert("考核发起成功！")
                                } else {
                                    layer.alert("考核发起失败！")
                                }
                                table.bootstrapTable('refresh', {});
                            }
                        })
                    })
                else{
                    layer.alert("请选择考核项目")
                }
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
            FastflowBase.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Config.billFileds.forEach(function (item) {
                if ($.inArray(item, Config.canEditFields) === -1) {
                    $('[name="row[' + item + ']"]').attr('disabled','disabled');
                }
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});